const qiniu = require("qiniu");
const accessKey = '8QQD0qX3ER_tMNfKMeYfueFECLJW1Zyg7zExska0';
const secretKey = 'T4fa8ULII7kOxqv9oCDRGhC3zb37vSKnXPtFYPQk';
const mac = new qiniu.auth.digest.Mac(accessKey, secretKey);

const options = {
    scope: "example_upload",
    returnBody: '{"url":"http://upload.hyfarsight.com/$(key)","code":0}',
    callbackBodyType: 'application/json'
};
const putPolicy = new qiniu.rs.PutPolicy(options);
const uploadToken = putPolicy.uploadToken(mac);
const config = new qiniu.conf.Config();
config.zone = qiniu.zone.Zone_z1;
let formUploader = new qiniu.form_up.FormUploader(config);
let putExtra = new qiniu.form_up.PutExtra();
// 文件上传
function upload(key, localFile) {
    return new Promise((resolve, reject) => {
        formUploader.putFile(uploadToken, key, localFile, putExtra, function(respErr,
            respBody, respInfo) {
            if (respErr) {
                reject(respErr);
            }
            if (respInfo.statusCode == 200) {
                resolve(respBody);
            }
        });
    })
}
module.exports = upload;